package com.macro.mall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmProdSpec;
import com.macro.mall.model.PmProduct;
import com.macro.mall.product.dto.PmProdSpecDto;
import com.macro.mall.product.dto.PmProductDto;
import com.macro.mall.product.dto.PmProductViewDto;
import com.macro.mall.product.service.PmProdSpecService;
import com.macro.mall.product.service.PmProductService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Slf4j
@Controller
@Api(tags = "PmProductController", description = "商品管理")
@RequestMapping("/product")
public class PmProductController {

    @Autowired
    private PmProdSpecService pmProdSpecService;
    @Autowired
    private PmProductService pmProductService;

    @Autowired
    private Mapper mapper;

    @ApiOperation(value = "商品列表--不分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmProdSpecDto>> list(@RequestParam(value = "prodSpecName", defaultValue = "") String prodSpecName,
                                                    @RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        PmProdSpecDto prodSpecDto = new PmProdSpecDto();
        if(!StringUtils.isBlank(prodSpecName)){
            prodSpecDto.setSpecName(prodSpecName);
        }
        if(serverSpecId != null){
            prodSpecDto.setServerSpecId(serverSpecId);
        }
        List<PmProdSpec> prodSpecList = pmProdSpecService.list(prodSpecDto);
        List<PmProdSpecDto> dtoList  = pmProdSpecService.converDataList(prodSpecList);
        return CommonResult.success(dtoList);
    }
    @ApiOperation(value = "商品列表--分页")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmProdSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "prodSpecName", defaultValue = "") String prodSpecName,
                                                            @RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        PmProdSpecDto prodSpecDto = new PmProdSpecDto();
        if(!StringUtils.isBlank(prodSpecName)){
            prodSpecName= URLDecoder.decode(prodSpecName);
            prodSpecDto.setSpecName(prodSpecName);
        }
        if(serverSpecId != null){
            prodSpecDto.setServerSpecId(serverSpecId);
        }
        List<PmProdSpec> prodSpecList = pmProductService.listPage(pageNum,pageSize,prodSpecDto);

        PageInfo<PmProdSpec> pageInfo = new PageInfo<PmProdSpec>(prodSpecList);

        List<PmProdSpecDto> dtoList  = pmProdSpecService.converDataList(prodSpecList);

        CommonPage<PmProdSpecDto> result = new CommonPage<PmProdSpecDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(dtoList);

        return CommonResult.success(CommonPage.restPage(dtoList));
    }
    @ApiOperation(value = "商品列表--组合条件分页")
    @RequestMapping(value = "/listPageByMixture", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Map<String,Object>>> listPageByMixture(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "productName", defaultValue = "") String productName,
                                                            @RequestParam(value = "channelId", defaultValue = "") Long channelId,
                                                            @RequestParam(value = "status", defaultValue = "") Integer status
    ) {
        if(!StringUtils.isBlank(productName)){
            productName= URLDecoder.decode(productName);
        }
        List<Map<String,Object>> productList = pmProductService.listPageByMixture(pageNum,pageSize,productName,channelId,status);

        return CommonResult.success(CommonPage.restPage(productList));
    }

    @ApiOperation(value = "商品新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@Validated @RequestBody PmProductDto pmProductDto, BindingResult result) {
        CommonResult commonResult;
        pmProductDto.setId(null);
        int count = pmProductService.save(pmProductDto);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "商品修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@Validated @RequestBody PmProductDto pmProductDto, BindingResult result) {
        CommonResult commonResult;
        int count = pmProductService.save(pmProductDto);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "商品状态修改")
    @RequestMapping(value = "/changeStatus", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult changeStatus(@RequestParam(value = "prodId", defaultValue = "") Long prodId ,
                                     @RequestParam(value = "prodCode", defaultValue = "") String prodCode,
                                     @RequestParam(value = "status", defaultValue = "") Integer status,
                                     @RequestParam(value = "updateDesc", defaultValue = "") String updateDesc
    ) {
        CommonResult commonResult;

        if(!StringUtils.isBlank(updateDesc)){
            updateDesc= URLDecoder.decode(updateDesc);
        }

        int count = pmProductService.changeStatus(prodId,prodCode,status,updateDesc);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "获取单个产品")
    @RequestMapping(value = "/getProductById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductDto> getProductById(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {
        PmProduct pmProduct = pmProductService.getById(prodId);
        PmProductDto pmProductDto= pmProductService.converDataInfo(pmProduct);
        return CommonResult.success(pmProductDto);
    }
    @ApiOperation(value = "获取单个产品视图")
    @RequestMapping(value = "/getProdViewById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductViewDto> getProdViewById(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {
        PmProduct pmProduct = pmProductService.getById(prodId);
        PmProductViewDto pmProductViewDto= pmProductService.converDataInfoView(pmProduct);
        return CommonResult.success(pmProductViewDto);
    }
    @ApiOperation(value = "商品配置，根据条件获取商品库存数据")
    @RequestMapping(value = "/getProdStockSetListShow", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<JSONObject> getProdStockSetListShow(@RequestParam(value = "skuCodes", defaultValue = "") String skuCodes,
                                                            @RequestParam(value = "releaseAreas", defaultValue = "") String releaseAreas,
                                                            @RequestParam(value = "prodSpecId", defaultValue = "") Long prodSpecId,
                                                            @RequestParam(value = "productId", defaultValue = "") Long productId,
                                                            @RequestParam(value = "channelId", defaultValue = "") Long channelId


    ) {
        log.info("商品配置，根据条件获取商品库存数据  PmProductController.getProdStockSetListShow  skuCodes="+skuCodes+"  releaseAreas="+releaseAreas+" prodSpecId"+prodSpecId+" productId="+productId+" channelId="+channelId);

        String msg="";
        if(StringUtils.isBlank(skuCodes)){
            msg =msg+"skuCode不能为空！";
            return CommonResult.failed("skuCode不能为空！");
        }
        if(StringUtils.isBlank(releaseAreas)){
            msg =msg+"skuCode不能为空！";
        }
        if(prodSpecId==null ||prodSpecId.longValue()==0){
            msg =msg+"skuCode不能为空！";
        }
        if(!StringUtils.isBlank(msg)){
            return CommonResult.failed(msg);
        }
        JSONObject retObj=null;
        try {
            retObj = pmProductService.getProdStockSetListShow(skuCodes, releaseAreas, prodSpecId, productId, channelId);
        }catch (Exception e){
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success(retObj);
    }
}
